package com.todod.base.module.infra.controller.admin.baseofficialletter;

import com.todod.base.module.infra.controller.admin.file.vo.file.FileRespVO;
import com.todod.base.module.infra.dal.dataobject.baseofficialletter.BaseOfficialLetterDetailDO;
import com.todod.base.module.infra.dal.dataobject.baseofficialletter.BaseOfficialLetterFileDO;
import com.todod.base.module.infra.dal.dataobject.baseofficialletter.BaseOfficialLetterRecordsDO;
import com.todod.base.module.infra.dal.dataobject.file.FileDO;
import com.todod.base.module.infra.dal.mysql.baseofficialletter.BaseOfficialLetterDetailMapper;
import com.todod.base.module.infra.dal.mysql.baseofficialletter.BaseOfficialLetterFileMapper;
import com.todod.base.module.infra.dal.mysql.baseofficialletter.BaseOfficialLetterRecordsMapper;
import com.todod.base.module.infra.dal.mysql.file.FileMapper;
import com.todod.base.module.system.api.user.AdminUserApi;
import com.todod.base.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;

import java.util.*;

import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import static com.todod.base.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import com.todod.base.module.infra.controller.admin.baseofficialletter.vo.*;
import com.todod.base.module.infra.dal.dataobject.baseofficialletter.BaseOfficialLetterDO;
import com.todod.base.module.infra.service.baseofficialletter.BaseOfficialLetterService;

@Tag(name = "管理后台 - 公函")
@RestController
@RequestMapping("/infra/base-official-letter")
@Validated
public class BaseOfficialLetterController {

    @Resource
    private BaseOfficialLetterService baseOfficialLetterService;
    @Resource
    private BaseOfficialLetterDetailMapper baseOfficialLetterDetailMapper;
    @Resource
    private BaseOfficialLetterFileMapper baseOfficialLetterFileMapper;
    @Resource
    private BaseOfficialLetterRecordsMapper baseOfficialLetterRecordsMapper;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private FileMapper fileMapper;


    // ----------------------------------------------公函管理页签---------------------------------------------------------
    @PostMapping("/create")
    @Operation(summary = "创建公函")
    public CommonResult<Long> createBaseOfficialLetter(@Valid @RequestBody BaseOfficialLetterSaveReqVO createReqVO) {
        return success(baseOfficialLetterService.createBaseOfficialLetter(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公函")
    public CommonResult<Boolean> updateBaseOfficialLetter(@Valid @RequestBody BaseOfficialLetterSaveReqVO updateReqVO) {
        baseOfficialLetterService.updateBaseOfficialLetter(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公函")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteBaseOfficialLetter(@RequestParam("id") Long id) {
        baseOfficialLetterService.deleteBaseOfficialLetter(id);
        return success(true);
    }

    @PutMapping("/submitOfficialLetter")
    @Operation(summary = "批量提交公函管理数据")
    public CommonResult<Long> submitOfficialLetter(@Valid @RequestBody BaseOfficialLetterSubmitReqVO submitReqVO) {
        return success(baseOfficialLetterService.submitOfficialLetter(submitReqVO));
    }

    @PutMapping("/updateOfficialDetail")
    @Operation(summary = "关联公函人员")
    public CommonResult<Boolean> updateOfficialDetail(@Valid @RequestBody BaseOfficialLetterDetailsSaveReqVO updateReqVO) {
        baseOfficialLetterService.updateOfficialDetail(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公函")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BaseOfficialLetterRespVO> getBaseOfficialLetter(@RequestParam("id") Long id) {
        BaseOfficialLetterDO baseOfficialLetter = baseOfficialLetterService.getBaseOfficialLetter(id);
        return success(BeanUtils.toBean(baseOfficialLetter, BaseOfficialLetterRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得公函分页")
    public CommonResult<PageResult<BaseOfficialLetterRespVO>> getBaseOfficialLetterPage(@Valid BaseOfficialLetterPageReqVO pageReqVO) {
        PageResult<BaseOfficialLetterDO> pageResult = baseOfficialLetterService.getBaseOfficialLetterPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseOfficialLetterRespVO.class));
    }

    @GetMapping("/getLetterUserList")
    @Operation(summary = "获取公函管理-接收人员列表信息")
    public CommonResult<List<BaseOfficialLetterDetailRespVO>> getLetterUserList(@RequestParam("officialLetterId") Long officialLetterId) {
        List<BaseOfficialLetterDetailDO> pageResult = baseOfficialLetterDetailMapper.getSimpleLetterDetailList(officialLetterId);
        pageResult.forEach(detail -> {
            AdminUserRespDTO startUser = adminUserApi.getUser(detail.getUserId()).getCheckedData();
            if (startUser != null) {
                detail.setUserName(startUser.getNickname());
                detail.setDeptId(startUser.getDeptId());
                detail.setMobile(startUser.getMobile());
            }
            BaseOfficialLetterDO baseOfficialLetter = baseOfficialLetterService.getBaseOfficialLetter(detail.getOfficialLetterId());
            detail.setPublishTime(baseOfficialLetter.getPublishTime());
        });
        return success(BeanUtils.toBean(pageResult, BaseOfficialLetterDetailRespVO.class));
    }

    @GetMapping("/getLetterFileList")
    @Operation(summary = "获取公函管理-公函附件列表信息")
    public CommonResult<List<FileRespVO>> getLetterFileList(@RequestParam("officialLetterId") Long officialLetterId) {
        List<BaseOfficialLetterFileDO> pageResult = baseOfficialLetterFileMapper.getSimpleLetterFileList(officialLetterId);
        List<FileDO> fileVOS = new ArrayList<>();
        pageResult.forEach(detail -> {
            FileDO file = fileMapper.selectById(detail.getFileId());
            if (file != null) {
                file.setUid(file.getId());
                fileVOS.add(file);
            }
        });
        return success(BeanUtils.toBean(fileVOS, FileRespVO.class));
    }


    // ----------------------------------------------有效、无效公函页签---------------------------------------------------------
    @GetMapping("/getEffectOfficialLetterPage")
    @Operation(summary = "有效、无效公函-查询公函分页")
    public CommonResult<PageResult<BaseOfficialLetterRespVO>> getEffectOfficialLetterPage(@Valid BaseOfficialLetterPageReqVO pageReqVO) {
        // 获取当前登录用户id添加到查询条件中
        pageReqVO.setUserId(getLoginUserId());
        PageResult<BaseOfficialLetterDO> pageResult = baseOfficialLetterService.getEffectOfficialLetterPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseOfficialLetterRespVO.class));
    }

    @PutMapping("/updateLetterDetail")
    @Operation(summary = "查阅时更新公函管理明细表数据")
    public CommonResult<Long> updateLetterDetail(@Valid @RequestBody BaseOfficialLetterDetailSaveReqVO createReqVO) {
        return success(baseOfficialLetterService.updateLetterDetail(createReqVO));
    }

    // ----------------------------------------------公函审批页签---------------------------------------------------------
    @GetMapping("/getInvalidOfficialLetterPage")
    @Operation(summary = "公函审批-查询待审批和已审批公函分页")
    public CommonResult<PageResult<BaseOfficialLetterRespVO>> getInvalidOfficialLetterPage(@Valid BaseOfficialLetterPageReqVO pageReqVO) {
        // 获取当前登录用户id添加到查询条件中
        pageReqVO.setUserId(getLoginUserId());
        PageResult<BaseOfficialLetterDO> pageResult = baseOfficialLetterService.getInvalidOfficialLetterPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseOfficialLetterRespVO.class));
    }

    @PostMapping("/createBaseOfficialRecord")
    @Operation(summary = "创建公函审批记录")
    public CommonResult<Long> createBaseOfficialRecord(@Valid @RequestBody BaseOfficialLetterRecordsSaveReqVO createReqVO) {
        return success(baseOfficialLetterService.createBaseOfficialRecord(createReqVO));
    }

    @GetMapping("/getLetterRecordsList")
    @Operation(summary = "获取公函管理-公函审批记录列表信息")
    public CommonResult<List<BaseOfficialLetterRecordsRespVO>> getLetterRecordsList(@RequestParam("officialLetterId") Long officialLetterId) {
        List<BaseOfficialLetterRecordsDO> list = baseOfficialLetterRecordsMapper.getSimpleLetterRecordsList(officialLetterId);
        return success(BeanUtils.toBean(list, BaseOfficialLetterRecordsRespVO.class));
    }

}